package com.education.project.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.education.project.model.LoginUser;
import com.education.project.service.LoginUserService;
import com.education.project.service.LoginUserTypeService;
import com.education.project.util.Page;
import com.education.project.web.form.LoginUserPasswordForm;

@Controller
@RequestMapping("/system/loginuser")
public class LoginUserController {
	@Autowired
	private LoginUserTypeService loginUserTypeService;
	@Autowired
	private LoginUserService loginUserService;
	
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public ModelAndView getIndexPage() {
		ModelAndView model = new ModelAndView("loginuser/index");
		List loginUserTypeList = loginUserTypeService.getAllLoginUserType();
		model.addObject("loginUserTypeList", loginUserTypeList);
		return model;
	}
	
	@RequestMapping(value="/index",method=RequestMethod.POST)
	public @ResponseBody Page getLoginUserByLimit(
			@RequestParam("page") Integer startPage,
			@RequestParam("rows") Integer limit,
			LoginUser param) {
		return loginUserService.getLoginUserByPage(param, startPage, limit);
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public @ResponseBody Map addLoginUser(
			@Valid @ModelAttribute("loginUser") LoginUser param,
			BindingResult binder) {
		Map result = new HashMap();
		if(!binder.hasErrors()) {
			LoginUser loginUser = loginUserService.getLoginUserByLoginname(param.getLoginname());
			if(loginUser!=null) {
				binder.rejectValue("loginname", "error", "增加失败,该用户名已经存在");
			}
		}
		if(binder.hasErrors()) {
			FieldError fieldError = binder.getFieldErrors().get(0);
			result.put("success", false);
			result.put("message", fieldError.getDefaultMessage());
			return result;
		}
		loginUserService.saveLoginUser(param);
		result.put("success", true);
		return result;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public @ResponseBody Map updateLoginUser(
			@Valid @ModelAttribute("loginUser") LoginUser param,
			BindingResult binder) {
		Map result = new HashMap();
		if(binder.hasErrors()) {
			FieldError fieldError = binder.getFieldErrors().get(0);
			result.put("success", false);
			result.put("message", fieldError.getDefaultMessage());
			return result;
		}
		loginUserService.updateLoginUser(param);
		result.put("success", true);
		return result;
	}
	
	@RequestMapping(value="/remove",method=RequestMethod.POST)
	public @ResponseBody Map removeLoginUser(@RequestParam("id") Integer id) {
		Map result = new HashMap();
		loginUserService.removeLoginUser(id);
		result.put("success", true);
		return result;
	}
	
	@RequestMapping(value="/reset_pwd",method=RequestMethod.POST)
	public @ResponseBody Map batchResetPwd(@RequestParam("ids") String ids) {
		Map result = new HashMap();
		List<Object[]> list = new ArrayList<Object[]>();
		String[] array = ids.split(",");
		for(String id:array) {
			list.add(new Object[]{new Integer(id)});
		}
		loginUserService.updatePwdBatch(list);
		result.put("success", true);
		return result;
	}
	
	@RequestMapping(value="/update_password",method=RequestMethod.POST)
	public @ResponseBody Map updateLoginUserPassword(
			@Valid @ModelAttribute("loginUserPasswordForm") LoginUserPasswordForm form,
			BindingResult binder,
			HttpServletRequest request) {
		Map result = new HashMap();
		if(!binder.hasErrors()) {
			LoginUser loginUser = (LoginUser) request.getSession().getAttribute("loginUser");
			Integer loginUserId = loginUserService.getLoginUserPassword(loginUser.getId(), form.getOld_password());
			if(loginUserId==null || loginUserId.intValue()!=loginUser.getId().intValue()) {
				binder.rejectValue("old_password", "error", "旧密码输入错误");
			}
			if(!binder.hasErrors() && !form.getPassword().equals(form.getAgin_password())) {
				binder.rejectValue("password", "error", "两次密码输入不一致");
			}
			if(!binder.hasErrors()) {
				loginUserService.updateLoginUserPassword(loginUser.getId(), form.getPassword());
			}
		}
		if(binder.hasErrors()) {
			FieldError fieldError = binder.getFieldErrors().get(0);
			result.put("success", false);
			result.put("message", fieldError.getDefaultMessage());
			return result;
		}
		result.put("success", true);
		return result;
	}
	
	@RequestMapping(value="/update_fullname",method=RequestMethod.POST)
	public @ResponseBody Map updateLoginUserFullname(
			@RequestParam("id") Integer id,
			@RequestParam("fullname") String fullname) {
		Map result = new HashMap();
		if(fullname.length()>20) {
			result.put("message", "姓名长度不能大于30");
			result.put("success", false);
			return result;
		}
		loginUserService.updateLoginUserFullname(id, fullname);
		result.put("success", true);
		return result;
	}
}
